package javatuning.ch3;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

@Warmup(iterations = 5,time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5,time = 5, timeUnit = TimeUnit.SECONDS)
@State(Scope.Benchmark)
public class FunctionProgram {

	@Benchmark
	@BenchmarkMode(Mode.Throughput)
	@Warmup(iterations = 2)
	@Measurement(iterations = 2)
	public int[] stream() throws IOException {
		int[] data = {1,2,3,4,5,6,7,8,9,10};
		return Arrays.stream(data).map(d->d/2).toArray();
	}

	@Benchmark
	@BenchmarkMode(Mode.Throughput)
	@Warmup(iterations = 2)
	@Measurement(iterations = 2)
	public int[] loop() {
		int[] data = {1,2,3,4,5,6,7,8,9,10};
		int size=data.length;
		for(int i=0;i<size;i++){
			data[i]/=2;
		}
		return data;
	}

	public static void main(String[] args) throws Exception {
        Options opt = new OptionsBuilder()
                .include(FunctionProgram.class.getSimpleName())
                .forks(1)
                .build();
        new Runner(opt).run();
	}
}
